﻿Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Partial Class Reports_report_group_user2
    Inherits System.Web.UI.Page

    Private report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        CrystalReportViewer1.ReportSource = report
        CrystalReportViewer1.PrintMode = CrystalDecisions.Web.PrintMode.ActiveX
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dw As report_group_userTableAdapters.USP_List_group_user3TableAdapter
        Dim dt As DataTable
        Dim strCode As String
        Dim strType As String
        Dim iType As Integer
        Dim ctx_User_Type As Integer
        Dim ctx_Department_Code As Integer
        Try
            'get Querystring
            iType = Request.QueryString("group_user_type")
            strCode = Request.QueryString("department_code")
            strType = Request.QueryString("str_group_user_type")
            'get Session
            ctx_User_Type = Session("group_user_type")
            ctx_Department_Code = Session("department_code")
            'check status , 0: ทั้งหมด  ,1: ใช้ , 2: ไม่ใช้
            strType = IIf(strType = "ประเภท", "ทั้งหมด", strType)

            dw = New report_group_userTableAdapters.USP_List_group_user3TableAdapter

            'Retrieve data from DB & collect to datatable
            dt = dw.GetData(iType, ctx_User_Type, ctx_Department_Code, strCode)

            'check 
            If (dt.Rows.Count > 0) Then
                'Create report engine
                'Dim report As New CrystalDecisions.CrystalReports.Engine.ReportDocument
                'map report parth
                report.Load(Server.MapPath("report_group_user.rpt"))
                'set data table to report
                report.SetDataSource(dt)
                'set input paratmenter
                report.SetParameterValue("Type", strType)

                'Show report
                'CrystalReportViewer1.ReportSource = report
            Else
                ' hide invalid message
                CrystalReportViewer1.Visible = False

                GenericHelper.ShowAlert(Me, "ไม่พบข้อมูล")
                Dim strscript As String = "<script language=javascript>window.top.close();</script>"
                If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                    Page.RegisterStartupScript("clientScript", strscript)
                End If
                ' TODO:
                ' Redirect to pervious page
            End If
        Catch ex As Exception
            ' hide invalid message
            CrystalReportViewer1.Visible = False

            GenericHelper.ShowAlert(Me, ex.Message)
            Dim strscript As String = "<script language=javascript>window.top.close();</script>"
            If (Not Page.IsStartupScriptRegistered("clientScript")) Then
                Page.RegisterStartupScript("clientScript", strscript)
            End If
        Finally
            If Not (dt Is Nothing) Then
                dt.Dispose()
            End If
            If Not (dw Is Nothing) Then
                dw.Dispose()
            End If
        End Try
    End Sub

    Protected Sub Page_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Unload
        If Not (CrystalReportViewer1 Is Nothing) Then
            CrystalReportViewer1.Dispose()
        End If
    End Sub

    Protected Sub CrystalReportViewer1_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles CrystalReportViewer1.Unload
        If Not (report Is Nothing) Then
            report.Close()
            report.Dispose()
        End If
    End Sub
End Class
